package com.himiotech.core.project.web.system.monitor;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.himiotech.core.framework.helper.anno.Log;
import com.himiotech.core.framework.helper.enums.BusinessType;
import com.himiotech.core.framework.helper.utils.ExcelUtil;
import com.himiotech.core.project.model.entity.OperateLog;
import com.himiotech.core.project.model.metadata.OperateLogMetadata;
import com.himiotech.core.project.service.IOperateLogService;
import com.himiotech.core.project.web.base.HimioBaseController;
import com.himiotech.core.project.web.base.model.HimioResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 操作日志记录
 *
 * @author ruoyi
 * @author wuwenbin
 */
@Controller
@RequestMapping("/monitor/operateLog")
public class OperateLogController extends HimioBaseController {

    @Autowired
    private IOperateLogService operateLogService;

    @RequiresPermissions("monitor:operateLog:view")
    @GetMapping()
    public String operlog() {
        return "monitor/operateLog";
    }

    /**
     * 查询操作日志记录列表
     *
     * @param page       分页信息
     * @param operateLog 查询条件
     * @return 表格分页数据
     */
    @RequiresPermissions("monitor:operateLog:list")
    @PostMapping("/list")
    @ResponseBody
    public HimioResult list(Page<OperateLog> page, OperateLog operateLog) {
        handleOrderBy(request, page);
        page = operateLogService.selectOperateLogListPage(page, operateLog);
        return writeJsonLayuiTable(page);
    }

    /**
     * 导出操作日志记录列表数据
     *
     * @param operateLog 查询条件
     * @return 导出数据
     */
    @Log(title = "操作日志记录 - 导出", businessType = BusinessType.EXPORT)
    @RequiresPermissions("monitor:operateLog:export")
    @PostMapping("/export")
    @ResponseBody
    public HimioResult export(OperateLog operateLog) {
        QueryWrapper<OperateLog> query = Wrappers.query();
        if (operateLog != null) {
            query.eq(ObjectUtil.isNotEmpty(operateLog.getTitle()), OperateLogMetadata.TITLE.getColName(), operateLog.getTitle());
            if (ArrayUtil.isNotEmpty(operateLog.getBusinessTypes())) {
                query.in(OperateLogMetadata.BUSINESS_TYPE.getColName(), Arrays.stream(operateLog.getBusinessTypes()).collect(Collectors.toList()));
            }
            query.eq(ObjectUtil.isNotEmpty(operateLog.getOperateType()), OperateLogMetadata.OPERATE_TYPE.getColName(), operateLog.getOperateType());
            query.eq(ObjectUtil.isNotEmpty(operateLog.getStatus()), OperateLogMetadata.STATUS.getColName(), operateLog.getStatus());
            query.like(ObjectUtil.isNotEmpty(operateLog.getOperateName()), OperateLogMetadata.OPERATE_NAME.getColName(), operateLog.getOperateName());
            query.eq(operateLog.getBusinessType() != null, OperateLogMetadata.BUSINESS_TYPE.getColName(), operateLog.getBusinessType());
            query.like(ObjectUtil.isNotEmpty(operateLog.getDeptName()), OperateLogMetadata.DEPT_NAME.getColName(), operateLog.getDeptName());
            Map<String, Object> searchParams = operateLog.getSearchParams();
            if (!searchParams.isEmpty()) {
                String operateTimeStart = MapUtil.getStr(searchParams, StrUtil.toCamelCase(OperateLogMetadata.OPERATE_TIME.getColName()).concat("Start"));
                String operateTimeEnd = MapUtil.getStr(searchParams, StrUtil.toCamelCase(OperateLogMetadata.OPERATE_TIME.getColName()).concat("End"));
                query.ge(StrUtil.isNotEmpty(operateTimeStart), OperateLogMetadata.OPERATE_TIME.getColName(), operateTimeStart);
                query.le(StrUtil.isNotEmpty(operateTimeEnd), OperateLogMetadata.OPERATE_TIME.getColName(), operateTimeEnd);
            }
        }
        List<OperateLog> list = operateLogService.list(query);
        ExcelUtil<OperateLog> util = new ExcelUtil<>(OperateLog.class);
        return util.exportExcel(list, "操作日志记录");
    }

    /**
     * 删除操作日志记录
     *
     * @param ids 删除id（以逗号分隔）
     * @return 处理结果
     */
    @RequiresPermissions("monitor:operateLog:remove")
    @Log(title = "操作日志记录 - 删除", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public HimioResult remove(String ids) {
        if (ids != null) {
            List<String> idList = Arrays.stream(ids.split(",")).collect(Collectors.toList());
            return writeJson(operateLogService.removeByIds(idList));
        } else {
            return writeJsonOk("没有需要删除的数据");
        }
    }


    @Log(title = "操作日志 - 清除", businessType = BusinessType.CLEAN)
    @RequiresPermissions("monitor:operateLog:remove")
    @PostMapping("/clean")
    @ResponseBody
    public HimioResult clean() {
        operateLogService.cleanOperateLog();
        return writeJsonOk();
    }
}
